//声明构造函数
function Promise(executor){
    // 声明状态属性
    this.PromiseState = 'pending';
    // 声明结果值属性
    this.PromiseResult = undefined;
    // 声明属性
    this.callbacks = [];

    //声明 success 函数
    let success = (value) => {
        //判断
        if(this.PromiseState !== 'pending') return;
        //作用:                       实例对象属性
        //1. 修改实例对象『状态』为成功  PromiseState
        this.PromiseState = 'fulfilled';
        //2. 设置成功的『结果值』       PromiseResult
        this.PromiseResult = value;
        //执行待执行的回调
        if(this.callbacks.length > 0){
            this.callbacks.forEach(cbObj => {
                //执行成功的回调
                cbObj.ok();
            })
        }
    }

    //声明 error 函数
    let error = (reason) => {
        if(this.PromiseState !== 'pending') return;

        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
        //执行失败的回调
        if(this.callbacks.length > 0){
            this.callbacks.forEach(cbObj => {
                //执行成功的回调
                cbObj.ng();
            })
        }
    }
    try{
        //执行 executor
        executor(success, error);
    }catch(e){
        //如果抛出错误, 修改 promise 状态为失败
        error(e);   
    }
}
//为实例对象添加 then 方法  on when 当...时候
Promise.prototype.then = function(onFulfilled, onRejected){
    //为 onRejected 设置默认值
    if(typeof onRejected !== 'function'){
        onRejected = reason => {
            throw reason;
        }
    }

    if(typeof onFulfilled !== 'function'){
        onFulfilled = value => value;
    }

    //执行对应的回调函数
    return new Promise((resolve, reject) => {
        //声明一个函数 『handle 处理的意思』
        let handle = (type) => {
            try{
                //获取回调函数的返回值
                let res = type(this.PromiseResult);
                //判断
                if(res instanceof Promise){
                    //2. 返回 promise 类型的值
                    res.then(v => {
                        resolve(v);
                    }, r => {
                        reject(r);
                    })
                }else{
                    //1. 返回非 promise 类型的值
                    resolve(res);
                }
            }catch(e){
                //3. 抛出错误/异常
                reject(e);
            }
        }
        //判断 promise 状态是否为成功
        if(this.PromiseState === 'fulfilled'){
            handle(onFulfilled);
        }
        //失败的情况
        if(this.PromiseState === 'rejected'){
            handle(onRejected);
        }
        //判断
        if(this.PromiseState === 'pending'){
            //将回调函数保存起来
            this.callbacks.push({
                //状态改为成功的时候, 执行
                ok: function(){
                    //执行成功的回调
                    handle(onFulfilled);
                },
                //状态改为失败的时候, 执行
                ng: function(){
                    handle(onRejected);
                }
            })
        }
    })
}

//为实例对象添加 catch 方法
Promise.prototype.catch = function(onRejected){
    //
    return this.then(undefined, onRejected);
}



// function alert(data){
//     console.log(data)
// }

// alert(123);